package com.novel.authorization.staff.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.novel.authorization.staff.entity.NfPerm;
import com.novel.authorization.staff.service.NfPermService;
import com.novel.base.entity.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 权限码表
 *
 * @author 樊温军
 * @date Wed May 07 11:00:49 CST 2025
 */
@Slf4j
@Api(tags = "权限码表", value = "权限码表")
@RestController
@AllArgsConstructor
@RequestMapping("/nfPerm")
public class NfPermController {

    private final NfPermService nfPermService;

    /**
     * 权限码表—列表
     *
     * @param nfPerm 权限码表
     * @return R<List < NfPerm>>
     */
    @SaCheckPermission("nfPerm:list")
    @ApiOperation(value = "权限码表—列表", notes = "权限码表—列表")
    @GetMapping("/list")
    public R<List<NfPerm>> getNfPermList(NfPerm nfPerm) {
        log.info("开始查询权限码表—列表，参数：{}", nfPerm);
        try {
            return R.ok(nfPermService.getNfPermList(nfPerm), "查询成功");
        } catch (Exception e) {
            log.error("查询权限码表—列表时发生错误：{}", e.getMessage(), e);
            return R.failed("查询权限码表—列表时发生错误，请稍后重试！");
        }
    }

    /**
     * 权限码表—列表—分页
     *
     * @param page   分页对象
     * @param nfPerm 权限码表
     * @return R<IPage < NfPerm>>
     */
    @SaCheckPermission("nfPerm:page")
    @ApiOperation(value = "权限码表—列表—分页", notes = "权限码表—列表—分页")
    @GetMapping(value = "/page")
    public R<IPage<NfPerm>> getNfPermPage(Page<NfPerm> page, NfPerm nfPerm) {
        log.info("开始查询权限码表—列表—分页，第 {} 页，每页 {} 条记录，其他参数：{}", page.getCurrent(), page.getSize(), nfPerm);
        try {
            IPage<NfPerm> iPage = nfPermService.getPage(page, nfPerm);
            return R.ok(iPage, "查询成功");
        } catch (Exception e) {
            log.error("查询权限码表—列表—分页时发生错误：{}", e.getMessage(), e);
            return R.failed("查询权限码表—列表—分页时发生错误，请稍后重试！");
        }
    }

    /**
     * 权限码表—保存
     *
     * @param nfPerm 权限码表
     * @return R<Long>
     */
    @SaCheckPermission("nfPerm:saveOne")
    @ApiOperation(value = "权限码表—保存", notes = "权限码表—保存")
    @PostMapping(value = "/saveOne")
    public R<Long> saveOneNfPerm(@Valid @RequestBody NfPerm nfPerm) {
        log.info("开始保存权限码表：{}", nfPerm);
        try {
            Long id = nfPermService.saveOneNfPerm(nfPerm);
            log.info("权限码表保存成功，ID: {}", id);
            return R.ok(id, "保存成功");
        } catch (Exception e) {
            log.error("保存权限码表时发生错误：{}", e.getMessage(), e);
            return R.failed("保存权限码表时发生错误，请稍后重试！");
        }
    }

    /**
     * 权限码表—删除
     *
     * @param id 权限码表主键
     * @return R<Boolean>
     */
    @SaCheckPermission("nfPerm:delById")
    @ApiOperation(value = "权限码表—删除", notes = "权限码表—删除")
    @PostMapping(value = "/delById")
    public R<Boolean> delByIdNfPerm(Long id) {
        log.info("开始删除权限码表：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            nfPermService.delById(id);
            return R.ok(Boolean.TRUE, "删除成功");
        } catch (Exception e) {
            log.error("删除权限码表失败: ID={}, Error={}", id, e.getMessage(), e);
            return R.failed("删除失败，请稍后重试！");
        }
    }

    /**
     * 权限码表—详情
     *
     * @param id 权限码表主键
     * @return R<NfPerm>
     */
    @SaCheckPermission("nfPerm:details")
    @ApiOperation(value = "权限码表—详情", notes = "权限码表—详情")
    @GetMapping(value = "/details")
    public R<NfPerm> getNfPerm(Long id) {
        log.info("开始查询权限码表：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            NfPerm nfPerm = nfPermService.queryById(id);
            if (nfPerm != null) {
                return R.ok(nfPerm, "查询成功");
            } else {
                return R.failed("未找到权限码表");
            }
        } catch (Exception e) {
            log.error("查询权限码表详情时发生错误：{}", e.getMessage(), e);
            return R.failed("查询权限码表详情时发生错误，请稍后重试！");
        }
    }
}
